*Create results*

clear

use "Results\tab_B3\Scenario_13\dataset_with_singles_and_immigrants.dta"

gen temp=string(final_educ)
gen temp2=substr(temp,4,1)

gen three_digit=0
replace three_digit=1 if missing(temp2)

drop temp temp2

drop if three_digit==1



//generate partners' ambition
by couple_id aar, sort: gen am_male = ambition if koen=="1" & relationship==1
by couple_id aar: gen am_female = ambition if koen=="2" & relationship==1
by couple_id aar: egen maxeduc = max(am_male)
by couple_id aar: replace am_male = maxeduc
drop maxeduc
by couple_id aar: egen maxeduc = max(am_female)
by couple_id aar: replace am_female = maxeduc
drop maxeduc


*Ambition
*first parts of 1980
forvalues i=1980(1)1980{
*table
qui tab am_male am_female if aar==`i' & koen=="1", matcell(mat_`i')
mat mat_`i'_2=mat_`i'/r(N)

*marginals
sca male_1_`i'=mat_`i'_2[1,1]+mat_`i'_2[1,2]+mat_`i'_2[1,3]+mat_`i'_2[1,4]
sca male_2_`i'=mat_`i'_2[2,1]+mat_`i'_2[2,2]+mat_`i'_2[2,3]+mat_`i'_2[2,4]
sca male_3_`i'=mat_`i'_2[3,1]+mat_`i'_2[3,2]+mat_`i'_2[3,3]+mat_`i'_2[3,4]
sca male_4_`i'=mat_`i'_2[4,1]+mat_`i'_2[4,2]+mat_`i'_2[4,3]+mat_`i'_2[4,4]

sca female_1_`i'=mat_`i'_2[1,1]+mat_`i'_2[2,1]+mat_`i'_2[3,1]+mat_`i'_2[4,1]
sca female_2_`i'=mat_`i'_2[1,2]+mat_`i'_2[2,2]+mat_`i'_2[3,2]+mat_`i'_2[4,2]
sca female_3_`i'=mat_`i'_2[1,3]+mat_`i'_2[2,3]+mat_`i'_2[3,3]+mat_`i'_2[4,3]
sca female_4_`i'=mat_`i'_2[1,4]+mat_`i'_2[2,4]+mat_`i'_2[3,4]+mat_`i'_2[4,4]

*unweighted likelihood indices
forvalues m=1(1)4{
	forvalues f=1(1)4{
sca s_`m'_`f'_`i'=mat_`i'_2[`m',`f']/(male_`m'_`i'*female_`f'_`i')
}
}

}

*then rest of loop
foreach i in 1981 2018{
*table
qui tab am_male am_female if aar==`i' & koen=="1", matcell(mat_`i')
mat mat_`i'_2=mat_`i'/r(N)

*marginals
sca male_1_`i'=mat_`i'_2[1,1]+mat_`i'_2[1,2]+mat_`i'_2[1,3]+mat_`i'_2[1,4]
sca male_2_`i'=mat_`i'_2[2,1]+mat_`i'_2[2,2]+mat_`i'_2[2,3]+mat_`i'_2[2,4]
sca male_3_`i'=mat_`i'_2[3,1]+mat_`i'_2[3,2]+mat_`i'_2[3,3]+mat_`i'_2[3,4]
sca male_4_`i'=mat_`i'_2[4,1]+mat_`i'_2[4,2]+mat_`i'_2[4,3]+mat_`i'_2[4,4]

sca female_1_`i'=mat_`i'_2[1,1]+mat_`i'_2[2,1]+mat_`i'_2[3,1]+mat_`i'_2[4,1]
sca female_2_`i'=mat_`i'_2[1,2]+mat_`i'_2[2,2]+mat_`i'_2[3,2]+mat_`i'_2[4,2]
sca female_3_`i'=mat_`i'_2[1,3]+mat_`i'_2[2,3]+mat_`i'_2[3,3]+mat_`i'_2[4,3]
sca female_4_`i'=mat_`i'_2[1,4]+mat_`i'_2[2,4]+mat_`i'_2[3,4]+mat_`i'_2[4,4]

*unweighted likelihood indices
forvalues m=1(1)4{
	forvalues f=1(1)4{
sca s_`m'_`f'_`i'=mat_`i'_2[`m',`f']/(male_`m'_`i'*female_`f'_`i')
}
}

*optimal lambda cf. paper with Bastian
sca xi1_`i'=((mat_`i'_2[1,1]/((1-male_4_`i'-male_3_`i'-male_2_`i')^2))-(mat_`i'_2[4,4]/((male_4_`i')^2)))*(male_4_`i'-male_4_1980)+((mat_`i'_2[1,1]/((1-male_4_`i'-male_3_`i'-male_2_`i')^2))-(mat_`i'_2[3,3]/((male_3_`i')^2)))*(male_3_`i'-male_3_1980)+((mat_`i'_2[1,1]/((1-male_4_`i'-male_3_`i'-male_2_`i')^2))-(mat_`i'_2[2,2]/((male_2_`i')^2)))*(male_2_`i'-male_2_1980)

sca xi2_`i'=((mat_`i'_2[1,1]/((1-female_4_`i'-female_3_`i'-female_2_`i')^2))-(mat_`i'_2[4,4]/((female_4_`i')^2)))*(female_4_`i'-female_4_1980)+((mat_`i'_2[1,1]/((1-female_4_`i'-female_3_`i'-female_2_`i')^2))-(mat_`i'_2[3,3]/((female_3_`i')^2)))*(female_3_`i'-female_3_1980)+((mat_`i'_2[1,1]/((1-female_4_`i'-female_3_`i'-female_2_`i')^2))-(mat_`i'_2[2,2]/((female_2_`i')^2)))*(female_2_`i'-female_2_1980)

gen temp=.
replace temp=0 if sign(xi1_`i')==sign(xi2_`i') & abs(xi1_`i')<abs(xi2_`i')
replace temp=(xi1_`i'/(xi1_`i'-xi2_`i')) if sign(xi1_`i')!=sign(xi2_`i')
replace temp=1 if sign(xi1_`i')==sign(xi2_`i') & abs(xi1_`i')>abs(xi2_`i')

sca lambda_`i'=temp
drop temp

*weights
forvalues m=1(1)4{
	forvalues f=1(1)4{
sca weight_`m'_`f'_`i'=lambda_`i'*male_`m'_`i'+(1-lambda_`i')*female_`f'_`i'
}
}

*weighted likelihood indices
forvalues m=1(1)4{
	forvalues f=1(1)4{
sca S_`m'_`f'_`i'=s_`m'_`f'_`i'*weight_`m'_`f'_`i'
}
}

*aggregate indices

sca diag_`i'=S_1_1_`i'+S_2_2_`i'+S_3_3_`i'+S_4_4_`i'

}

*finish 1980 part

forvalues i=1980(1)1980{
	
*weights
forvalues m=1(1)4{
	forvalues f=1(1)4{
sca weight_`m'_`f'_`i'=lambda_1981*male_`m'_`i'+(1-lambda_1981)*female_`f'_`i'
}
}

*weighted likelihood indices
forvalues m=1(1)4{
	forvalues f=1(1)4{
sca S_`m'_`f'_`i'=s_`m'_`f'_`i'*weight_`m'_`f'_`i'
}
}

*aggregate indices

sca diag_`i'=S_1_1_`i'+S_2_2_`i'+S_3_3_`i'+S_4_4_`i'

}

**Add population marignals incl. singles and diagonal pi's

foreach i in 1980 1981 2018{
*marginals
sca male_1_`i'_marr=mat_`i'[1,1]+mat_`i'[1,2]+mat_`i'[1,3]+mat_`i'[1,4]
sca male_2_`i'_marr=mat_`i'[2,1]+mat_`i'[2,2]+mat_`i'[2,3]+mat_`i'[2,4]
sca male_3_`i'_marr=mat_`i'[3,1]+mat_`i'[3,2]+mat_`i'[3,3]+mat_`i'[3,4]
sca male_4_`i'_marr=mat_`i'[4,1]+mat_`i'[4,2]+mat_`i'[4,3]+mat_`i'[4,4]

sca female_1_`i'_marr=mat_`i'[1,1]+mat_`i'[2,1]+mat_`i'[3,1]+mat_`i'[4,1]
sca female_2_`i'_marr=mat_`i'[1,2]+mat_`i'[2,2]+mat_`i'[3,2]+mat_`i'[4,2]
sca female_3_`i'_marr=mat_`i'[1,3]+mat_`i'[2,3]+mat_`i'[3,3]+mat_`i'[4,3]
sca female_4_`i'_marr=mat_`i'[1,4]+mat_`i'[2,4]+mat_`i'[3,4]+mat_`i'[4,4]

qui tab ambition if koen=="1" & relationship==0 & aar==`i', matcell(am_single_m_`i')
sca male_1_`i'_sing=am_single_m_`i'[1,1]
sca male_2_`i'_sing=am_single_m_`i'[2,1]
sca male_3_`i'_sing=am_single_m_`i'[3,1]
sca male_4_`i'_sing=am_single_m_`i'[4,1]

qui tab ambition if koen=="2" & relationship==0 & aar==`i', matcell(am_single_f_`i')
sca female_1_`i'_sing=am_single_f_`i'[1,1]
sca female_2_`i'_sing=am_single_f_`i'[2,1]
sca female_3_`i'_sing=am_single_f_`i'[3,1]
sca female_4_`i'_sing=am_single_f_`i'[4,1]

sca am_pi_1_`i'=mat_`i'[1,1]/((male_1_`i'_sing*female_1_`i'_sing)^(1/2))
sca am_pi_2_`i'=mat_`i'[2,2]/((male_2_`i'_sing*female_2_`i'_sing)^(1/2))
sca am_pi_3_`i'=mat_`i'[3,3]/((male_3_`i'_sing*female_3_`i'_sing)^(1/2))
sca am_pi_4_`i'=mat_`i'[4,4]/((male_4_`i'_sing*female_4_`i'_sing)^(1/2))

}

**Add share of power couples
foreach i in 1980 1981 2018{
sca power_couple_`i'=mat_`i'_2[4,4]
}


frame copy default temp
frame change temp
gen temp=.
collapse (first) temp, by(aar)
drop temp

gen diag=.
foreach i in 1980 1981 2018{
replace diag=diag_`i' if aar==`i' 
}

forvalues m=1(1)4{
	forvalues f=1(1)4{
gen s_`m'_`f'=.
foreach i in 1980 1981 2018{
replace s_`m'_`f'=s_`m'_`f'_`i' if aar==`i' 
}
}
}

gen lambda=.
replace lambda=lambda_1981 if aar==1980 
foreach i in 1981 2018{
replace lambda=lambda_`i' if aar==`i' 
}

forvalues m=1(1)4{
	forvalues f=1(1)4{
gen weight_`m'_`f'=.
foreach i in 1980 1981 2018{
replace weight_`m'_`f'=weight_`m'_`f'_`i' if aar==`i' 
}
}
}

forvalues m=1(1)4{
	forvalues f=1(1)4{
gen S_`m'_`f'=.
foreach i in 1980 1981 2018{
replace S_`m'_`f'=S_`m'_`f'_`i' if aar==`i' 
}
}
}

forvalues m=1(1)4{
gen male_`m'=.
foreach i in 1980 1981 2018{
replace male_`m'=male_`m'_`i' if aar==`i' 
}
}

forvalues f=1(1)4{
gen female_`f'=.
foreach i in 1980 1981 2018{
replace female_`f'=female_`f'_`i' if aar==`i' 
}
}

forvalues m=1(1)4{
gen male_`m'_marr=.
foreach i in 1980 1981 2018{
replace male_`m'_marr=male_`m'_`i'_marr if aar==`i' 
}
}

forvalues m=1(1)4{
gen male_`m'_sing=.
foreach i in 1980 1981 2018{
replace male_`m'_sing=male_`m'_`i'_sing if aar==`i' 
}
}

forvalues m=1(1)4{
gen female_`m'_marr=.
foreach i in 1980 1981 2018{
replace female_`m'_marr=female_`m'_`i'_marr if aar==`i' 
}
}

forvalues m=1(1)4{
gen female_`m'_sing=.
foreach i in 1980 1981 2018{
replace female_`m'_sing=female_`m'_`i'_sing if aar==`i' 
}
}



forvalues m=1(1)4{
gen am_pi_`m'=.
foreach i in 1980 1981 2018{
replace am_pi_`m'=am_pi_`m'_`i' if aar==`i' 
}
}


gen power_couple=.
foreach i in 1980 1981 2018{
replace power_couple=power_couple_`i' if aar==`i' 
}


save "Results\tab_B3\Scenario_13\Sorting\results_ambition.dta", replace
